﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class TR_TrainingCurriculumsDtls : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private Main main = new Main();
        private SsMng creatby = new SsMng();
        InitialData_BLL iniData = new InitialData_BLL();
        private TrainingCoreCompetencyBLL CreatTrainingCoreCompetency = new TrainingCoreCompetencyBLL();
        private IHRMIndividualDevPlanBLL CreateIndividualDevPlan = new HRMIndividualDevPlanBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblID.Text = Request.QueryString["id"];
                Session["id"] = Request.QueryString["id"];

                bindHeader();

                LoadDataGrid();
                LoadDataGrid1();
                LoadDataGrid2();

                LoadTrainingCoreCompetency();
                ChangeMode1(Main.EditMode.View);

                cboCoreCompetencyType.DataSource = CreatTrainingCoreCompetency.LoadCoreCompetencyType();
                cboCoreCompetencyType.DataBind();
                cboCoreCompetencyType.Items.Insert(0, new RadComboBoxItem("กรุณาเลือกประเภทสมรรถนะ", ""));

                int? intCoreCompentencyTypeSeq = main.ConvertNullInt(cboCoreCompetencyType.SelectedValue);
                cboProficiency.DataSource = CreateIndividualDevPlan.LoadProficiency(intCoreCompentencyTypeSeq);
                cboProficiency.DataBind();
                cboProficiency.Items.Insert(0, new RadComboBoxItem("กรุณาเลือกระดับสมรรถนะ", ""));

                cboCompentencyLevel.DataSource = CreatTrainingCoreCompetency.LoadCoreCompetency();
                cboCompentencyLevel.DataBind();
                cboCompentencyLevel.Items.Insert(0, new RadComboBoxItem("กรุณาเลือกระดับสมรรถนะ", ""));

            }

        }

        private void bindHeader()
        {
            DataSet ds = new DataSet();
            ds = iniData.ListTrainingCurriculums(lblID.Text);
            lblName.Text = ds.Tables[0].Rows[0]["training_cur_descr"].ToString();
            lblType.Text = ds.Tables[0].Rows[0]["training_type_descr"].ToString();
            lblDetail.Text = ds.Tables[0].Rows[0]["details"].ToString();
        }

        private void LoadDataGrid()
        {
            try
            {
                DataSet ListSalaryCommand = iniData.ListTrainingCurriculumsPrerequisites("", lblID.Text);
                DataTable table = ListSalaryCommand.Tables[0];
                RadGrid2.DataSource = ListSalaryCommand.Tables[0];
                RadGrid2.DataBind();
            }
            catch (Exception ex)
            {

            }
        }

        protected void RadGrid2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);
                iniData.DeleteTrainingCurriculumsPrerequisites(seq);
            }
            if (e.CommandName == "ExpandCollapse")
                return;
            LoadDataGrid();
        }

        protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        {

        }


        private void LoadDataGrid1()
        {
            try
            {
                DataSet ListSalaryCommand = iniData.ListTrainingCurriculumsSubject("", lblID.Text);
                DataTable table = ListSalaryCommand.Tables[0];
                RadGrid1.DataSource = ListSalaryCommand.Tables[0];
                RadGrid1.DataBind();
            }
            catch (Exception ex)
            {

            }
        }

        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);
                iniData.DeleteTrainingCurriculumsSubject(seq);
            }
            if (e.CommandName == "ExpandCollapse")
                return;
            LoadDataGrid1();
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {

        }


        private void LoadDataGrid2()
        {
            try
            {
                DataSet ListSalaryCommand = iniData.ListTrainingCurriculumsCompare("", lblID.Text);
                DataTable table = ListSalaryCommand.Tables[0];
                RadGrid3.DataSource = ListSalaryCommand.Tables[0];
                RadGrid3.DataBind();
            }
            catch (Exception ex)
            {

            }
        }
        protected void RadGrid3_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);
                iniData.DeleteTrainingCurriculumsCompare(seq);
            }
           LoadDataGrid2();
        }

        protected void RadGrid3_ItemDataBound(object sender, GridItemEventArgs e)
        {

        }

        //======================================== ข้อมูลสมรรถนะ  =================================================  

        protected void ChangeMode1(Main.EditMode mode)
        {
            switch (mode)
            {
                case Main.EditMode.View:
                    pnlCoreCompetency.Visible = true;
                    pnlCoreCompetencyAdd.Visible = false;
                    pnlCoreCompetencyType.Visible = false;
                    btnSave.Visible = false;
                    btnEdit.Visible = false;
                    btnCancle.Visible = false;
                    break;
                case Main.EditMode.AddNew:
                    pnlCoreCompetency.Visible = false;
                    pnlSelectCoreControl.Visible = false;
                    pnlCoreCompetencyAdd.Visible = true;
                    pnlCoreCompetencyType.Visible = true;
                    btnSave.Visible = true;
                    btnEdit.Visible = false;
                    btnCancle.Visible = true;
                    break;

                case Main.EditMode.Edit:
                    pnlCoreCompetency.Visible = false;
                    pnlCoreCompetencyAdd.Visible = true;
                    pnlCoreCompetencyType.Visible = true;
                    btnSave.Visible = false;
                    btnEdit.Visible = true;
                    btnCancle.Visible = true;
                    break;
            }
        }

        private void ClearControlData()
        {
            cboCoreCompetencyType.SelectedIndex = 0;
            cboProficiency.SelectedIndex = 0;
            cboCompentencyLevel.SelectedIndex = 0;
        }

        private bool CheckNullValue()
        {
            bool bOk = true;

            if (cboCoreCompetencyType.SelectedIndex == 4)
            {
                if (cboCoreCompetencyType.SelectedValue.Length == 0)
                {
                    main.MessageAlert(Page, "กรุณาเลือกประเภทสมรรถนะ");
                    bOk = false;
                }
                else if (cboCompentencyLevel.SelectedValue.Length == 0)
                {
                    main.MessageAlert(Page, "กรุณาเลือกระดับสมรรถนะ");
                    bOk = false;
                }
            }
            else
            {
                if (cboCoreCompetencyType.SelectedValue.Length == 0)
                {
                    main.MessageAlert(Page, "กรุณาเลือกประเภทสมรรถนะ");
                    bOk = false;
                }
                else if (cboProficiency.SelectedValue.Length == 0)
                {
                    main.MessageAlert(Page, "กรุณาเลือกระดับสมรรถนะ");
                    bOk = false;
                }
                
            }
            
            return bOk;
        }

        protected void dgtCoreCompetency_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            lblID.Text = Request.QueryString["id"];
            int? ID = main.ConvertNullInt(lblID.Text);
            int? intCurrCompetencySeq = main.ConvertNullInt(lblCurrCompetency.Text);

            dgtCoreCompetency.DataSource = CreatTrainingCoreCompetency.Search(intCurrCompetencySeq, null, null, null, ID);
        }

        protected void dgtCoreCompetency_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "GridDelete")
            {
                GridDataItem item = (GridDataItem)e.Item;

                int? intCurrCompetencySeq = main.ConvertNullInt(item["curriculum_competency_seq"].Text);

                CreatTrainingCoreCompetency.Delete(intCurrCompetencySeq);
                LoadTrainingCoreCompetency();
                dgtCoreCompetency.DataBind();
                ChangeMode1(Main.EditMode.View);
            }
            else if (e.CommandName == "GridEdit")
            {
                ChangeMode1(Main.EditMode.Edit);
                GridDataItem item = (GridDataItem)e.Item;
                
                LoadCoreCompetencyType();
                pnlSelectCoreControl.Visible = false;

                if(cboCoreCompetencyType.SelectedIndex == 4)
                {

                    lblID.Text = Request.QueryString["id"];
                    lblCurrCompetency.Text = item["curriculum_competency_seq"].Text;
                    cboCoreCompetencyType.SelectedValue = item["competency_type_seq"].Text;
                    cboCoreCompetencyType.DataSource = CreatTrainingCoreCompetency.LoadCoreCompetencyType();
                    cboCoreCompetencyType.DataBind();
                    cboCoreCompetencyType.Items.Insert(0, new RadComboBoxItem("กรุณาเลือกประเภทสมรรถนะ", ""));
                    ViewState["competency_type_seq"] = item["competency_type_seq"].Text;
                    cboCompentencyLevel.SelectedValue = item["competency_ref_seq"].Text;
                   
                }
                else
                {
                    
                    lblID.Text = Request.QueryString["id"];
                    lblCurrCompetency.Text = item["curriculum_competency_seq"].Text;
                    cboCoreCompetencyType.SelectedValue = item["competency_type_seq"].Text;
                    cboCoreCompetencyType.DataSource = CreatTrainingCoreCompetency.LoadCoreCompetencyType();
                    cboCoreCompetencyType.DataBind();
                    cboCoreCompetencyType.Items.Insert(0, new RadComboBoxItem("กรุณาเลือกประเภทสมรรถนะ", ""));
                    ViewState["competency_type_seq"] = item["competency_type_seq"].Text;
                    cboProficiency.Items.Insert(0, new RadComboBoxItem("กรุณาเลือกระดับสมรรถนะ", ""));
                    cboProficiency.SelectedValue = item["competency_ref_seq"].Text;
                    
                }
            }
        }

        private void LoadTrainingCoreCompetency()
        {
            lblID.Text = Request.QueryString["id"];
            int? ID = main.ConvertNullInt(lblID.Text);
            int? intCurrCompetencySeq = main.ConvertNullInt(lblCurrCompetency.Text);
            dgtCoreCompetency.DataSource = CreatTrainingCoreCompetency.Search(intCurrCompetencySeq, null, null, null, ID);
        }

        private void LoadCoreCompetencyType()
        {
            cboCoreCompetencyType.DataSource = CreatTrainingCoreCompetency.LoadCoreCompetencyType();
            cboCoreCompetencyType.DataBind();
            cboCoreCompetencyType.Items.Insert(0, new RadComboBoxItem("กรุณาเลือกประเภทสมรรถนะ", ""));
        }

        protected void cboCoreCompetencyType_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (cboCoreCompetencyType.SelectedIndex > -1)
            {
                if (cboCoreCompetencyType.SelectedIndex == 4)
                {
                    pnlSelectCoreControl.Visible = true;
                    pnlSelectCore1.Visible = true;
                    pnlSelectCore.Visible = false;

                    cboCompentencyLevel.DataSource = CreatTrainingCoreCompetency.LoadCoreCompetency();
                    cboCompentencyLevel.DataBind();
                    cboCompentencyLevel.Items.Insert(0, new RadComboBoxItem("กรุณาเลือกระดับสมรรถนะ", ""));
                }
                else
                {
                    pnlSelectCoreControl.Visible = true;
                    pnlSelectCore1.Visible = false;
                    pnlSelectCore.Visible = true;

                    int? intCoreCompentencyTypeSeq = main.ConvertNullInt(cboCoreCompetencyType.SelectedValue);
                    cboProficiency.DataSource = CreateIndividualDevPlan.LoadProficiency(intCoreCompentencyTypeSeq);
                    cboProficiency.DataBind();
                    cboProficiency.Items.Insert(0, new RadComboBoxItem("กรุณาเลือกสมรรถนะ", ""));
                }
            }
        }

        protected void AddNew_Click(object sender, EventArgs e)
        {
            ClearControlData();
            ChangeMode1(Main.EditMode.AddNew);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int? intCoreCompentencyTypeSeq = main.ConvertNullInt(cboCoreCompetencyType.SelectedValue);
            int? ID = main.ConvertNullInt(lblID.Text);
            int? intProficiencySeq = main.ConvertNullInt(cboProficiency.SelectedValue);
            int? intCompentencyLevel = main.ConvertNullInt(cboCompentencyLevel.SelectedValue);
            int? intCurrCompetencySeq = main.ConvertNullInt(lblCurrCompetency.Text);
            if (!CheckNullValue())
            {
                return;
            }

            bool CheckUnique = CreatTrainingCoreCompetency.CheckUnique(ID, intCoreCompentencyTypeSeq);

            if (CheckUnique == false)
            {
                bool CheckValueSubmit = CreatTrainingCoreCompetency.CheckValueSubmit(intCurrCompetencySeq);
                if (CheckValueSubmit == true)
                {
                    if (cboCoreCompetencyType.SelectedIndex == 4)
                    {
                        CreatTrainingCoreCompetency.Insert(intCoreCompentencyTypeSeq, ID, intCompentencyLevel,
                                                           creatby.EmpSeqI);
                        LoadTrainingCoreCompetency();
                        dgtCoreCompetency.DataBind();
                    }
                    else
                    {
                        CreatTrainingCoreCompetency.Insert1(intCoreCompentencyTypeSeq, ID, intProficiencySeq,
                                                           creatby.EmpSeqI);
                        LoadTrainingCoreCompetency();
                        dgtCoreCompetency.DataBind();
                    }

                    main.MessageAlert(Page, "บันทึกข้อมูลเรียบร้อยแล้ว");
                    ClearControlData();
                    ChangeMode1(Main.EditMode.View);
                }
                else
                {
                    main.MessageAlert(Page, "ตรวจสอบข้อมูลอีกครั้ง");
                }

            }
            else
            {
                main.MessageAlert(Page, "ประเภทสมรรถนะต้องไม่ซ้ำกัน");
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int? ID = main.ConvertNullInt(lblID.Text);
            int? intCoreCompentencyTypeSeq = main.ConvertNullInt(cboCoreCompetencyType.SelectedValue);
            int? intCompentencyLevel = main.ConvertNullInt(cboCompentencyLevel.SelectedValue);
            int? intProficiencySeq = main.ConvertNullInt(cboProficiency.SelectedValue);
            int? intCurrCompetencySeq = main.ConvertNullInt(lblCurrCompetency.Text);
            bool CheckUnique = false;
            if (!CheckNullValue())
            {
                return;
            }

            int? intDiffCoreCompentencyTypeSeq = main.ConvertNullInt(ViewState["competency_type_seq"]);

            if (intDiffCoreCompentencyTypeSeq != intCoreCompentencyTypeSeq)
            {
                CheckUnique = CreatTrainingCoreCompetency.CheckUnique(ID, intCoreCompentencyTypeSeq);
            }

            if (!CheckUnique)
            {

                    if (cboCoreCompetencyType.SelectedIndex == 4)
                    {
                        CreatTrainingCoreCompetency.Update(intCoreCompentencyTypeSeq, ID, intCompentencyLevel,
                                                           creatby.EmpSeqI, intCurrCompetencySeq);
                        LoadTrainingCoreCompetency();
                        dgtCoreCompetency.DataBind();
                    }
                    else
                    {
                        CreatTrainingCoreCompetency.Update1(intCoreCompentencyTypeSeq, ID, intProficiencySeq,
                                                            creatby.EmpSeqI, intCurrCompetencySeq);
                        LoadTrainingCoreCompetency();
                        dgtCoreCompetency.DataBind();
                    }

                bool CheckValueSubmit = CreatTrainingCoreCompetency.CheckValueSubmit(intCurrCompetencySeq);
                if (CheckValueSubmit)
                {
                    main.MessageAlert(Page, "แก้ไขข้อมูลเรียบร้อยแล้ว");
                    ClearControlData();
                    ChangeMode1(Main.EditMode.View);
                }
                else
                {
                    main.MessageAlert(Page, "ตรวจสอบข้อมูลอีกครั้ง");
                }
            }
            else
            {
                main.MessageAlert(Page, "ประเภทสมรรถนะต้องไม่ซ้ำกัน");
            }
        }

        protected void btnCancle_Click(object sender, EventArgs e)
        {
            pnlCoreCompetencyAdd.Visible = false;
            pnlCoreCompetency.Visible = true;
            LoadTrainingCoreCompetency();
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            ViewState["update"] = Session["update"];
        }

    }
}
